package serive;

import Dao.UserDao;
import Dao.UserDaoImpl;
import entity.Mail;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao Udao = new UserDaoImpl();

    @Override
    public boolean login(String id, String pwd) {
        String rPsw = Udao.login(id);
        if (rPsw != null && rPsw.equals(pwd)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean register(String uId, String password1, String password2, String idCard, String sex,
                            int age, String address, String uName, String telephone) {
        //用户名:
        //必须是6-10位字母、数字、下划线(这里字母、数字、下划线是指任意组合，没有必须三类均包含)
        //不能以数字开头
        String idRegex = "^[^0-9][\\w_]{5,9}$";

        //密码:
        //必须是6-20位的字母、数字、下划线(这里字母、 数字下划线是指任意组合,没有必须三类均包含)
        String pswRegex = "^[\\w_]{6,20}$";
        boolean matchesId = uId.matches(idRegex);
        boolean matchesPsw = password1.matches(pswRegex);

        if (password1.equals(password2)) {
            if (matchesId && matchesPsw) {
                Boolean b = Udao.register(uId, password1, idCard, sex, age, address, uName, telephone);
                if (b) {
                    return true;
                } else {
                    return false;
                }
            }
        }
        return false;
    }

    @Override
    public boolean modify(String uId, String password1,String password2, String idCard, String sex, int age,
                          String address, String uName, String telephone) {
        if (password1.equals(password2)){
            boolean isModify = Udao.modifyUser(uId, password1, idCard, sex, age, address, uName, telephone);
            return isModify;
        }else return false;

    }

    @Override
    public List<Mail> queryMail(String uId) {
        return Udao.queryMail(uId);
    }

    public String queryAddress(String uId) {
        return Udao.queryAddress(uId);
    }
    public String queryTelephone(String uId) {
        return Udao.queryTelephone(uId);
    }
}
